package com.liujit.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.upms.domain.model.SysOauthClient;
import com.liujit.upms.domain.params.SysOauthClientParams;
import com.liujit.upms.domain.query.SysOauthClientQuery;
import com.liujit.upms.domain.result.SysOauthClientResult;
import com.liujit.upms.mapper.SysOauthClientMapper;
import com.liujit.upms.service.SysOauthClientService;
import org.springframework.stereotype.Service;

/**
 * @Description 系统认证客户端Service实现类
 * @Author liujun
 * @Date 2021-07-02 15:00:06
 */
@Service
public class SysOauthClientServiceImpl extends ServiceImpl<SysOauthClientMapper, SysOauthClient> implements SysOauthClientService {

    @Override
    public PageBean<SysOauthClientResult> getPage(SysOauthClientQuery query) {
        Page<SysOauthClient> page = Pagination.page(query);
        LambdaQueryWrapper<SysOauthClient> wrapper = new LambdaQueryWrapper();
        if (query.getEnabled() != null) {
            wrapper.eq(SysOauthClient::getEnabled, query.getEnabled());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(SysOauthClient::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(SysOauthClient::getCreateTime, query.getEndTime());
        }
        IPage<SysOauthClient> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, SysOauthClientResult.class);
    }

    @Override
    public Boolean save(SysOauthClientParams params) {
        SysOauthClient entity = DozerConvertor.convertor(params, SysOauthClient.class);
        // 添加
        if (params.getId() == null) {
            return super.save(entity);
        } else {
            SysOauthClient sysOauthClient = super.getById(params.getId());
            if (sysOauthClient == null) {
                throw new DataNotFoundException();
            }
            return super.updateById(entity);
        }
    }

    @Override
    public SysOauthClientResult get(Long id) {
        SysOauthClient sysOauthClient = super.getById(id);
        if (sysOauthClient == null) {
            throw new DataNotFoundException();
        }
        return DozerConvertor.convertor(sysOauthClient, SysOauthClientResult.class);
    }

    @Override
    public Boolean del(BaseParams params) {
        return super.removeById(params.getId());
    }

    @Override
    public Boolean enabled(BaseParams params) {
        SysOauthClient sysOauthClient = super.getById(params.getId());
        if (sysOauthClient == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new SysOauthClient() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_YES);
        }});
    }

    @Override
    public Boolean disabled(BaseParams params) {
        SysOauthClient sysOauthClient = super.getById(params.getId());
        if (sysOauthClient == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new SysOauthClient() {{
            setId(params.getId());
            setEnabled(GlobalConstant.ENABLED_NO);
        }});
    }

    @Override
    public Integer getCountByClientId(String clientId) {
        LambdaQueryWrapper<SysOauthClient> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysOauthClient::getClientId, clientId);
        return super.count(wrapper);
    }

    @Override
    public SysOauthClient getByClientId(String clientId) {
        LambdaQueryWrapper<SysOauthClient> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysOauthClient::getClientId, clientId);
        wrapper.eq(SysOauthClient::getEnabled, GlobalConstant.ENABLED_YES);
        return super.getOne(wrapper);
    }
}
